/*
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the FxStruts MailReader Application.
 *
 * The Initial Developer of the Original Code is
 * Anirudh Sasikumar (http://anirudhs.chaosnet.org/).
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
*/

package com.adobe.mailreader.command
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    import com.adobe.mailreader.business.ResourcesDelegate;
    import com.adobe.mailreader.events.ResourcesEvent;
    import com.adobe.mailreader.model.MailModelLocator;
    
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import mx.collections.ArrayCollection;

    /* Get the result of fx:message tags, that is, the actual
     * localized strings to display in the UI */
    public class ResourcesCommand implements ICommand, IResponder
    {
        public function ResourcesCommand()
        {
            
        }
        
        public function execute( event:CairngormEvent ):void
        {
            var rEvt:ResourcesEvent = event as ResourcesEvent;
            
            if ( rEvt )
            {
                var rDelegate:ResourcesDelegate = new ResourcesDelegate(this);
                rDelegate.getResources();
            }
            else
                trace("bad event to resourcescommand");
        }
        
        
        public function result( event : Object ) : void
        {				
            trace("Got result " + event.result);
            
            var model : MailModelLocator = MailModelLocator.getInstance(); 
            if ( event.result is Object && event.result.hasOwnProperty("errors") )
            {
                model.notifyView(MailModelLocator.MODEL_RESOURCES_GET_FAILED, event.result.errors);
            }
            else if ( event.result is ArrayCollection )
            {
                var arrcnt:int = 0;
                /* save the strings into our model */
                model.logonTitle = event.result[arrcnt++];
                model.promptUsername = event.result[arrcnt++];
                model.promptPassword = event.result[arrcnt++];
                model.registrationTitleCreate = event.result[arrcnt++];
                model.registrationTitleEdit = event.result[arrcnt++];
                model.promptPassword2 = event.result[arrcnt++];
                model.promptFullName = event.result[arrcnt++];
                model.promptFromAddress = event.result[arrcnt++];
                model.promptReplyToAddress = event.result[arrcnt++];
                model.buttonSave = event.result[arrcnt++];
                model.buttonCancel = event.result[arrcnt++];
                model.buttonConfirm = event.result[arrcnt++];
                model.headingSubscriptions = event.result[arrcnt++];
                model.headingHost = event.result[arrcnt++];
                model.headingUser = event.result[arrcnt++];
                model.headingType = event.result[arrcnt++];
                model.headingAutoconnect = event.result[arrcnt++];
                model.headingAction = event.result[arrcnt++];
                model.registrationDeleteSubscription = event.result[arrcnt++];
                model.registrationEditSubscription = event.result[arrcnt++];
                model.registrationAddSubscription = event.result[arrcnt++];
                model.subscriptionTitleCreate = event.result[arrcnt++];
                model.subscriptionTitleEdit = event.result[arrcnt++];
                model.subscriptionTitleDelete = event.result[arrcnt++];
                model.promptMailHostname = event.result[arrcnt++];
                model.promptMailUsername = event.result[arrcnt++];
                model.promptMailPassword = event.result[arrcnt++];
                model.promptMailServerType = event.result[arrcnt++];
                model.promptAutoConnect = event.result[arrcnt++];
                model.promptLogon = event.result[arrcnt++];
                model.promptTitle = event.result[arrcnt++];
                model.promptRegister = event.result[arrcnt++];
                model.promptLogoff = event.result[arrcnt++];
   
                model.notifyView(MailModelLocator.MODEL_RESOURCES_GET_SUCCESS);
            }        
        }
        
        public function fault( event : Object ) : void
        {
            trace("Got fault" + event);
            var model : MailModelLocator = MailModelLocator.getInstance();
            
            model.notifyView(MailModelLocator.MODEL_RESOURCES_GET_FAILED, (event as FaultEvent).fault.faultString);
        }
        
    }
}